#!/usr/bin/env bash
# --------------------( LICENSE                            )--------------------
# Copyright (c) 2014-2025 Beartype authors.
# See "LICENSE" for further details.
#
# --------------------( SYNOPSIS                           )--------------------
# Bash shell script statically type-checking this project with pyright, passing
# sane default options suitable for interactive terminal testing and otherwise
# passing all passed arguments as is to the "tox" command.
#
# This script is defined as a Bash rather than Bourne script purely for the
# canonical ${BASH_SOURCE} string global, reliably providing the absolute
# pathnames of this script and hence this script's directory.
#
# --------------------( CAVEATS                            )--------------------
# *tHIS SCRIPT ONLY STATICALLY TYPE-CHECKS THIS PROJECT'S CODEBASE.* Notably,
# this script avoids statically type-checking this project's test suite as
# well. Why? Because we only statically type-check this project's codebase for
# PEP 561-compliance and downstream consumers themselves statically
# type-checking beartype-dependent codebases. No such concerns apply to this
# test suite, which is clearly *NEVER* intended for external reuse.
#
# This test suite also conditionally statically type-checks this project's
# codebase as a functional test when the "pyright" command is in the current
# ${PATH} shell environment variable. While useful as a sanity check, this
# script is typically preferable from the human-readable persective as its
# output is significantly more readable than that captured by that test.

# ....................{ PREAMBLE                           }....................
# Enable strictness for sanity.
set -e

# ....................{ FUNCTIONS                          }....................
# str canonicalize_path(str pathname)
#
# Canonicalize the passed pathname.
function canonicalize_path() {
    # Validate and localize all passed arguments.
    (( $# == 1 )) || {
        echo 'Expected exactly one argument.' 1>&2
        return 1
    }
    local pathname="${1}"

    # The "readlink" command's GNU-specific "-f" option would be preferable but
    # is unsupported by macOS's NetBSD-specific version of "readlink". Instead,
    # just defer to Python for portability.
    command python3 -c "
import os, sys
print(os.path.realpath(os.path.expanduser(sys.argv[1])))" "${pathname}"
}

# ....................{ PATHS                              }....................
# Absolute or relative filename of this script.
script_filename="$(canonicalize_path "${BASH_SOURCE[0]}")"

# Absolute or relative dirname of the directory directly containing this
# script, equivalent to the top-level directory for this project.
script_dirname="$(dirname "${script_filename}")"

# ....................{ MAIN                               }....................
# Temporarily change the current working directory to that of this project.
pushd "${script_dirname}" >/dev/null

#FIXME: Note that we *COULD* additionally pass the "--verifytypes" option, which
#exposes further "pyright" compliants. Let's avoid doing so until someone
#explicitly requests we do so, please. This has dragged on long enough! *HURK*
# Statically type-check this project's codebase with all passed arguments.
# command pyright beartype "${@}"
# command pyright --pythonversion 3.10 beartype "${@}"
# command pyright --pythonversion 3.11 beartype "${@}"
# command pyright --pythonversion 3.13 beartype "${@}"
command pyright --pythonversion 3.14 beartype "${@}"
# command pyright --pythonversion 3.15 beartype "${@}"

# 0-based exit code reported by the prior command.
exit_code=$?

# Revert the current working directory to the prior such directory.
popd >/dev/null

# Report the same exit code from this script.
exit ${exit_code}
